package com.shop.service.impl;

import com.shop.mapper.GoodsMapper;
import com.shop.model.Goods;
import com.shop.model.User;
import com.shop.model.common.GoodsShowPage;
import com.shop.model.common.SearchPageGoods;
import com.shop.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Slf4j
    @Service
    public class GoodsServiceImpl implements GoodsService {
        @Autowired
        private GoodsMapper goodsMapper;
        @Autowired
    private RedisService redisService;

    @Override
    public void goodsAdd(Map<String, Object> params) {
        ObjectMapper mapper = new ObjectMapper();
        Goods goods = mapper.convertValue(params, Goods.class);
        if(goods.getNewest()==null) goods.setNewest("N");
        if(goods.getHottest()==null) goods.setHottest("N");
        goods.setSaleTime(new Date());
        goods.setStatus("Y");
        log.info("goodsAdd:{}", goods);
        goodsMapper.insert(goods);
        redisService.delete("","","");
    }

    @Override
    public List<Goods> selectAll() {
        return goodsMapper.selectAll();
    }

    @Override
    public Integer searchCount(SearchPageGoods searchPage) {
        return goodsMapper.searchCount(searchPage);
    }

    @Override
    public List<Goods> searchPage(SearchPageGoods searchPage) {
        int offSet=(searchPage.getPage()-1)* searchPage.getPageSize();
        searchPage.setOffSet(offSet);
        log.info("searchPage:{}", searchPage);
        List<Goods> list=goodsMapper.searchPage(searchPage);
        log.info("userList:{}",list);
        return list;
    }

    @Override
    public void update(Map map) {
        map.remove("file");
        ObjectMapper mapper=new ObjectMapper();
        Goods goods=mapper.convertValue(map, Goods.class);
        if(goods.getHottest()==null) goods.setHottest("N");
        if(goods.getNewest()==null) goods.setNewest("N");
        goodsMapper.update(goods);
        redisService.delete("","","");
    }

    @Override
    public void updateStatus(Integer id, String status) {
        goodsMapper.updateStatus(id, status);
        redisService.delete("","","");
    }

    @Override
    public List<Goods> showPage(GoodsShowPage goodsShowPage) {
        log.info("goodsShowPage:{}", goodsShowPage);
        int offSet = (goodsShowPage.getPage() - 1) * goodsShowPage.getPageSize();
        goodsShowPage.setOffSet(offSet);
        if(goodsShowPage.getLowestPrice()==null&&goodsShowPage.getHighestPrice()==null&&goodsShowPage.getName()==null&&goodsShowPage.getStatus()==null&&goodsShowPage.getCategoryId()==null){
            List<Goods> goodsList=redisService.getCacheGoodsPage(goodsShowPage.getPage());
            if(goodsList==null) {
                goodsList = goodsMapper.showPage(goodsShowPage);
                redisService.cacheGoodsPage(goodsShowPage.getPage(), goodsList);
                log.info("缓存第"+ goodsShowPage.getPage() +"页数据");
                return goodsList;
            }
            return goodsList;
        }

        if(goodsShowPage.getLowestPrice()==null&&goodsShowPage.getHighestPrice()==null&&goodsShowPage.getName()==null&&goodsShowPage.getStatus()!=null&&goodsShowPage.getCategoryId()==null){
            List<Goods> goodsList=redisService.getCacheGoodsHot(goodsShowPage.getStatus(),goodsShowPage.getPage());
            if(goodsList==null) {
                goodsList = goodsMapper.showPage(goodsShowPage);
                redisService.cacheGoodsHot(goodsShowPage.getStatus(),goodsShowPage.getPage(),goodsList);
                log.info("缓存第"+goodsShowPage.getStatus()+"下的"+ goodsShowPage.getPage() +"页数据");
                return goodsList;
            }
            return goodsList;
        }

        if (goodsShowPage.getLowestPrice()==null&&goodsShowPage.getHighestPrice()==null&&goodsShowPage.getName()==null&&goodsShowPage.getStatus()==null&&goodsShowPage.getCategoryId()!=null){
            List<Goods> goodsList=redisService.getCacheGoodsCategory(goodsShowPage.getCategoryId().get(goodsShowPage.getCategoryId().size()-1),goodsShowPage.getPage());
            if(goodsList==null) {
                goodsList = goodsMapper.showPage(goodsShowPage);
                redisService.cacheGoodsCategory(goodsShowPage.getCategoryId().get(goodsShowPage.getCategoryId().size()-1),goodsShowPage.getPage(),goodsList);
                log.info("缓存id:"+goodsShowPage.getCategoryId().get(goodsShowPage.getCategoryId().size()-1)+"分类下的第"+ goodsShowPage.getPage() +"页数据");
                return goodsList;
            }
            return goodsList;
        }

        return goodsMapper.showPage(goodsShowPage);
    }

    @Override
    public Integer showPageCount(GoodsShowPage goodsShowPage) {
        return goodsMapper.showPageCount(goodsShowPage);
    }

    @Override
    public Goods selectById(Integer id) {
        return goodsMapper.selectById(id);
    }

    @Override
    public Goods findById(int goodid) {
        return goodsMapper.selectById(goodid);
    }

    @Override
    public void reduceStock(int goodid, int num) {
        goodsMapper.reduceStock(goodid, num);
    }
}
